home *** CD-ROM | disk | FTP | other *** search
- /****i* SOURCE_FILE/INFO
- *
- * NAME
- * NumberFormat.js
- *
- * USAGE
- * Part of Netobjects JavaScript Library.
- *
- * COPYRIGHT
- * Copyright ⌐ 2000-2005 Website Pros, Inc.
- * All Rights Reserved.
- *
- * This is an unpublished work protected by Website Pros, Inc.
- * as a trade secret, and is not to be used or disclosed except as
- * expressly provided in a written license agreement executed by
- * you and Website Pros, Inc.
- *
- * <copyright@websitepros.com>
- *
- * NOTES
- * JavaScript code.
- *
- *****/
- if (!IS.isModuleInitialized("IS.NOF.TEXT.NumberFormat"))
- {
-
- /****h* NOF_JavaScript_Library/NOF.TEXT.NumberFormat
- *
- * NAME
- * NOF.TEXT.NumberFormat
- *
- * DESCRIPTION
- *
- * <code>NumberFormat</code> is a class which provides a simple way to
- * format a number using locale specific separators.
- * Usage sample:
- *
- * var myLocale = new NOF.UTIL.Locale("de", "DE");
- * var nf = new NOF.TEXT.NumberFormat(myLocale, "#,##0.##");
- * var str = nf.format(-13003.4567);
- * alert( str );
- *
- * External dependencies: NOF.UTIL.Locale, NOF.UTIL.DefaultLocale, NOF.UTIL.ResourceBundle, NOF.UTIL.Exception
- ****/
-
- /**
- * Constructor
- **/
- function TEXT_NumberFormat(/*NOF.UTIL.Locale*/ locale, /*String*/ pattern) {
- this.__proto__ = TEXT_NumberFormat.prototype;
-
- this.locale = locale;
- this.pattern = pattern;
-
- this.NumberFormatSymbols = null;
-
- }
- {
- var method = TEXT_NumberFormat.prototype;
-
- method.SYMBOLS_RESOURCE = "lib/nof/text/NumberFormatSettings";
-
- method.TEXT_NumberFormatSymbols = function () {
- this.decimalSeparator = arguments[0];
- this.groupingSeparator = arguments[1];
- this.patternSeparator = arguments[2];
- this.minusSign = arguments[3];
- this.percent = arguments[4];
- this.zeroDigit = arguments[5]; //see Arabic
- this.digit = arguments[6];
- this.exponential = arguments[7];
- this.perMill = arguments[8];
- this.infinity = arguments[9];
- this.NN = arguments[10];
- this.defaultPattern = arguments[11];
- }
-
- method.getNumberFormatSymbols = function(/*NOF.UTIL.Locale*/ locale) {
- if (locale == null) {
- locale = this.locale;
- }
- var propSymbols = NOF.UTIL.ResourceBundle.getBundle(this.SYMBOLS_RESOURCE, locale);
- if (propSymbols == null) {
- return null;
- } else {
- return new this.TEXT_NumberFormatSymbols(
- propSymbols.getProperty("decimalSeparator"),
- propSymbols.getProperty("groupingSeparator"),
- propSymbols.getProperty("patternSeparator"),
- propSymbols.getProperty("minusSign"),
- propSymbols.getProperty("percent"),
- propSymbols.getProperty("zeroDigit"),
- propSymbols.getProperty("digit"),
- propSymbols.getProperty("exponential"),
- propSymbols.getProperty("perMill"),
- propSymbols.getProperty("infinity"),
- propSymbols.getProperty("NaN"),
- propSymbols.getProperty("defaultPattern")
- );
- }
- }
-
- /**
- * Set locale
- * @param locale - specific locale
- **/
- method.setLocale = function (/*NOF.UTIL.Locale*/ locale) {
- if ( (locale != null) && (this.locale.equals(locale) == false) ) {
- this.locale = locale;
- this.NumberFormatSymbols = this.getNumberFormatSymbols(locale);
- }
-
- }
- /**
- * Get locale
- * @return locale
- **/
- /*NOF.UTIL.Locale*/ method.getLocale = function () {
- return this.locale;
- }
-
- /**
- * Set pattern
- * @param pattern
- **/
- method.setPattern = function (/*String*/ pattern) {
- if ( (pattern != null) && (this.pattern != pattern) ) {
- this.pattern = pattern;
- }
- }
- /**
- * Get pattern
- * @return pattern
- **/
- /*String*/ method.getPattern = function () {
- return this.pattern;
- }
-
- /**
- * Format a number.
- * @param n the number
- * @param locale (optional) - specific locale. Does not change current locale.
- * @return the string obtained by formating the number n
- **/
- /*String*/ method.format = function (/*Number*/ n, /*NOF.UTIL.Locale*/ locale) {
- if (n == null) return null;
- var nfs = null;
- if (locale != null) {
- nfs = this.getNumberFormatSymbols(locale);
- } else { //no locale argument
- if (this.NumberFormatSymbols == null) { //first call
- locale = (this.locale != null) ? this.locale : NOF.UTIL.DefaultLocale;
- this.NumberFormatSymbols = this.getNumberFormatSymbols(locale);
- }
- nfs = this.NumberFormatSymbols;
- }
- //alert("nfs.decimalSeparator = " + nfs.decimalSeparator);
- var str = "" + n;
-
- var isNegative = (str.charAt(0) == '-');
- var minusAtBegining = true;
-
- var pattern = this.pattern;
- if (pattern == null || pattern.trim().length == 0) {
- pattern = nfs.defaultPattern;
- }
- if (pattern.indexOf(nfs.patternSeparator) > -1) {
- if (isNegative == true) {
- pattern = pattern.substring(pattern.indexOf(nfs.patternSeparator) + 1, pattern.length);
- var iMinusInPattern = pattern.indexOf("-"); //should be > -1
- minusAtBegining = ( iMinusInPattern < pattern.indexOf(nfs.digit) );
- pattern = pattern.substring(0, iMinusInPattern) + pattern.substring(iMinusInPattern + 1, pattern.length);
-
- } else {
- pattern = pattern.substring(0, pattern.indexOf(nfs.patternSeparator));
- }
- }
-
- if (isNegative == true) {
- str = str.substring(1, str.length);
- }
-
- var in_intPart = "" + parseInt(str);
- var in_floatPart = "0";
- if (str.indexOf(".") > -1) {
- in_floatPart = str.substring(str.indexOf(".") + 1, str.length);
- }
-
- //#,##0.## eval("1.E"+1)
- var out_intPart, out_floatPart;
-
- var i = pattern.indexOf(/*nfs.decimalSeparator*/ ".");
- var floatPartLength = 0;
- if (i > -1) {
- //floatPartLength = pattern.length - (i + 1);
- floatPartLength = pattern.lastIndexOf(nfs.digit) - i;
- }
- out_floatPart = "" + Math.round( eval("1." + in_floatPart + "E" + floatPartLength) );
- out_floatPart = out_floatPart.substring(1, out_floatPart.length);
- //alert("pattern " + pattern + " floatPartLength = "+floatPartLength);
-
- var groupLength = (i > -1) ? (i - pattern.lastIndexOf(/*nfs.groupingSeparator*/ ",") - 1): 3;
- //alert("groupLength " + groupLength);
-
- var out_intPartArr = [];
- while(in_intPart.length > 0) {
- i = in_intPart.length - groupLength;
- if (i > 0) {
- tmpStr = in_intPart.substring(i, in_intPart.length);
- in_intPart = in_intPart.substring(0, i);
- } else {
- tmpStr = in_intPart;
- in_intPart = "";
- }
-
- out_intPartArr[out_intPartArr.length] = tmpStr;
- }
-
- out_intPart = out_intPartArr.reverse().join(nfs.groupingSeparator);
-
- str = (out_intPart + nfs.decimalSeparator + out_floatPart);
- if (isNegative == true) {
- str = (minusAtBegining == true) ? (nfs.minusSign + str) : (str + nfs.minusSign);
- }
-
- return str;
- }
-
- /**
- * get a number from a formatted string
- * @param source
- * @param locale (optional) - specific locale. Does not change current locale.
- * @return the number (as a float)
- **/
- /*Number*/ method.parse = function (/*String*/ source, /*NOF.UTIL.Locale*/ locale) {
- if (source == null || source.trim().length == 0) {
- return null; //should throw exception
- }
-
- var nfs = null;
- if (locale != null) {
- nfs = this.getNumberFormatSymbols(locale);
- } else { //no locale argument
- if (this.NumberFormatSymbols == null) { //first call
- locale = (this.locale != null) ? this.locale : NOF.UTIL.DefaultLocale;
- this.NumberFormatSymbols = this.getNumberFormatSymbols(locale);
- }
- nfs = this.NumberFormatSymbols;
- }
- var i = source.indexOf(nfs.minusSign);
- var iDec = source.indexOf(nfs.decimalSeparator);
- var minusAtBegining = true;
-
- var isNegative = (i > -1);
- if (isNegative == true) {
- source = source.substring(0, i) + source.substring(i+1, source.length);
- if (i > iDec) {
- minusAtBegining = false;
- } else {
- iDec--;
- }
- }
-
- var intPart = "", floatPart = "0";
- if (iDec == -1) {
- intPart = source;
- } else {
- intPart = source.substring(0, iDec);
- floatPart = source.substring(iDec + 1, source.length);
- }
-
- //var reGroup = /[nfs.groupingSeparator]/;
- //while(intPart.indexOf(nfs.groupingSeparator) > 0) {
- // intPart = intPart.replace(reGroup, "");
- //}
- intPart = (intPart.split(nfs.groupingSeparator)).join("");
-
- var n = parseFloat(intPart + "." + floatPart);
-
- if (isNegative == true) {
- n *= -1;
- }
- return n;
- }
- }
- // add it to NOF.TEXT namespace
- TEXT.__proto__.NumberFormat = TEXT_NumberFormat;
- }